//
//  AppDelegate.m
//  PerfectProduct
//
//  Created by TangXiaoFeng on 16/5/28.
//  Copyright © 2016年 TangXiaoFeng. All rights reserved.
//

#import "AppDelegate.h"
#import "HomeMainViewController.h"
#import "DiscoveryMainViewController.h"
#import "InformationMainViewController.h"
#import "MineMainViewController.h"
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKConnector/ShareSDKConnector.h>

//腾讯开放平台（对应QQ和QQ空间）SDK头文件
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>

//微信SDK头文件
#import "WXApi.h"

//新浪微博SDK头文件
#import "WeiboSDK.h"
//新浪微博SDK需要在项目Build Settings中的Other Linker Flags添加"-ObjC"

//sun
#define DEF_Guide_count 5
@interface AppDelegate ()<UIScrollViewDelegate>
@property (nonatomic,assign)BOOL isOut;
@property (nonatomic,strong)UIImageView *imageView;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    [ShareSDK registerApp:@"iosv1101"
     
          activePlatforms:@[
                            @(SSDKPlatformTypeSinaWeibo),
                            @(SSDKPlatformTypeWechat),
                            @(SSDKPlatformTypeQQ),
                                                        ]
                 onImport:^(SSDKPlatformType platformType)
     {
         switch (platformType)
         {
             case SSDKPlatformTypeWechat:
                 [ShareSDKConnector connectWeChat:[WXApi class]];
                 break;
             case SSDKPlatformTypeQQ:
                 [ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]];
                 break;
             case SSDKPlatformTypeSinaWeibo:
                 [ShareSDKConnector connectWeibo:[WeiboSDK class]];
                 break;
             
             default:
                 break;
         }
     }
          onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo)
     {
         
         switch (platformType)
         {
             case SSDKPlatformTypeSinaWeibo:
                 //设置新浪微博应用信息,其中authType设置为使用SSO＋Web形式授权
                 [appInfo SSDKSetupSinaWeiboByAppKey:@"568898243"
                                           appSecret:@"38a4f8204cc784f81f9f0daaf31e02e3"
                                         redirectUri:@"http://www.sharesdk.cn"
                                            authType:SSDKAuthTypeBoth];
                 break;
             case SSDKPlatformTypeWechat:
                 [appInfo SSDKSetupWeChatByAppId:@"wx4868b35061f87885"
                                       appSecret:@"64020361b8ec4c99936c0e3999a9f249"];
                 break;
             case SSDKPlatformTypeQQ:
                 [appInfo SSDKSetupQQByAppId:@"100371282"
                                      appKey:@"aed9b0303e3ed1e27bae87c33761161d"
                                    authType:SSDKAuthTypeBoth];
                 break;
                          default:
                 break;
         }
     }];
    
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
    HomeMainViewController *homeMainViewController = [[HomeMainViewController alloc] init];
    homeMainViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"Home"] tag:101];
    _homeNavigationController = [[UINavigationController alloc] initWithRootViewController:homeMainViewController];
    
    DiscoveryMainViewController *discoveryMainViewController = [[DiscoveryMainViewController alloc] init];
    discoveryMainViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"发现" image:[UIImage imageNamed:@"Discovery"] tag:102];
    _discoveryNavigationController = [[UINavigationController alloc] initWithRootViewController:discoveryMainViewController];
    
    InformationMainViewController *informationMainViewController = [[InformationMainViewController alloc] init];
    informationMainViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"消息" image:[UIImage imageNamed:@"Information"] tag:103];
    _informationNavigationController = [[UINavigationController alloc] initWithRootViewController:informationMainViewController];
    
    MineMainViewController *mineMainViewController = [[MineMainViewController alloc] init];
    mineMainViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@"Person"] tag:104];
    _mineNavigationController = [[UINavigationController alloc] initWithRootViewController:mineMainViewController];
    
    _tabBarController = [[UITabBarController alloc] init];
    _tabBarController.viewControllers = @[_homeNavigationController, _discoveryNavigationController, _informationNavigationController, _mineNavigationController];
    _window.rootViewController = _tabBarController;
    [self.window makeKeyAndVisible];
    
    
    //  启动页
    _isOut = NO;
    //  在沙盒做一个文件，判断沙盒里有没有这个文件
    NSFileManager *manager = [NSFileManager defaultManager];
    BOOL isHasFile = [manager fileExistsAtPath:[NSHomeDirectory() stringByAppendingString:@"pic.txt"]];
    if (isHasFile) {
        [self gotoMain];
    }else{
        [self makeLaunchView];
    }
    return YES;
}
#pragma mark---去主页---
-(void)gotoMain
{
    //  第一次没有这个文件，我们就创建这个文件
    NSFileManager *manager = [NSFileManager defaultManager];
    //  判断我们是否创建了文件，如果没创建，就创建这个文件（这种情况就运行一次，也就是第一次启动程序的时候）
    if (![manager fileExistsAtPath:[NSHomeDirectory() stringByAppendingString:@"pic.txt"]]) {
        [manager createFileAtPath:[NSHomeDirectory() stringByAppendingString:@"pic.txt"] contents:nil attributes:nil];
    }
    
}
#pragma mark---假引导界面---
-(void)makeLaunchView
{
    //通过scrollView 将这些图片添加在上面，从而达到滚动这些图片
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.window.bounds];
    scrollView.contentSize = CGSizeMake(Kwidth * DEF_Guide_count, Kheight);
    scrollView.delegate = self;
    scrollView.pagingEnabled = YES;
   [self.window addSubview:scrollView];
    for (int i = 0; i < DEF_Guide_count; i++) {
        _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(Kwidth * i, 0, Kwidth, Kheight)];
        _imageView.userInteractionEnabled = YES;
        _imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"IMG_045%d",(i + 3)]];
        [scrollView addSubview:_imageView];
        
        UIButton *QQbutton = [UIButton buttonWithType:UIButtonTypeCustom];
                
        QQbutton.frame = CGRectMake(144 , Kheight - 120 , 50, 50);
        [QQbutton addTarget:self action:@selector(QQbeginClick:) forControlEvents:UIControlEventTouchUpInside];
        [_imageView addSubview:QQbutton];
        
        UIButton *WeiXinButton = [UIButton buttonWithType:UIButtonTypeCustom];
        WeiXinButton.frame = CGRectMake(66, Kheight - 120 , 50, 50);
        [WeiXinButton addTarget:self action:@selector(WeiXinbeginClick:) forControlEvents:UIControlEventTouchUpInside];
       [_imageView addSubview:WeiXinButton];
        
        UIButton *XinLangButton = [UIButton buttonWithType:UIButtonTypeCustom];
        XinLangButton.frame = CGRectMake(220, Kheight - 120, 50, 50);
        [XinLangButton addTarget:self action:@selector(XinLangbeginClick:) forControlEvents:UIControlEventTouchUpInside];
        [_imageView addSubview:XinLangButton];
        
        
        UIButton *CasualLookButton = [UIButton buttonWithType:UIButtonTypeCustom];
        CasualLookButton.frame = CGRectMake(300, Kheight - 120, 50, 50);
        [CasualLookButton addTarget:self action:@selector(CasualLookbeginClick:) forControlEvents:UIControlEventTouchUpInside];
        [_imageView addSubview:CasualLookButton];
        

        
        
    }

}
#pragma mark---QQ登入---
-(void)QQbeginClick:(UIButton *)button
{
    
    //例如QQ的登录
     [ShareSDK getUserInfo:SSDKPlatformTypeQQ
           onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error)
     {
         if (state == SSDKResponseStateSuccess)
         {
             
             NSLog(@"uid=%@",user.uid);
             NSLog(@"%@",user.credential);
             NSLog(@"token=%@",user.credential.token);
             NSLog(@"nickname=%@",user.nickname);
         }
         
         else
         {
             NSLog(@"%@",error);
         }
         
     }];
}
#pragma mark---微信登入---
-(void)WeiXinbeginClick:(UIButton *)button
{
    
    [ShareSDK getUserInfo:SSDKPlatformTypeWechat
           onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error)
     {
         if (state == SSDKResponseStateSuccess)
         {
             
             NSLog(@"uid=%@",user.uid);
             NSLog(@"%@",user.credential);
             NSLog(@"token=%@",user.credential.token);
             NSLog(@"nickname=%@",user.nickname);
         }
         
         else
         {
             NSLog(@"%@",error);
         }
         
     }];
}
#pragma mark---新浪登入---
-(void)XinLangbeginClick:(UIButton *)button
{
    
    [ShareSDK getUserInfo:SSDKPlatformTypeSinaWeibo
           onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error)
     {
         if (state == SSDKResponseStateSuccess)
         {
             
             NSLog(@"uid=%@",user.uid);
             NSLog(@"%@",user.credential);
             NSLog(@"token=%@",user.credential.token);
             NSLog(@"nickname=%@",user.nickname);
         }
         
         else
         {
             NSLog(@"%@",error);
         }
         
     }];
}

#pragma mark---随便看看---
-(void)CasualLookbeginClick:(UIButton *)button
{
    
}


#pragma mark---ScrollView的代理---
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x == 0) {
        scrollView.bounces = NO;
    }else{
        scrollView.bounces = YES;
    }

    if (scrollView.contentOffset.x > Kwidth * (DEF_Guide_count - 1) + 20 ) {
        _isOut = YES;
    }
}
#pragma mark---ScrollView停止滑动---
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (_isOut == YES) {
        [UIView animateWithDuration:1 animations:^{
            scrollView.alpha = 0;
        } completion:^(BOOL finished) {
            [scrollView removeFromSuperview];
            [self gotoMain];
        }];
    }
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
